package com.dhl.cloud.core.service;


import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dhl.cloud.common.http.HttpUser;
import com.dhl.cloud.core.dao.RoleMapper;
import com.dhl.cloud.core.dao.UserAndRoleMapper;
import com.dhl.cloud.core.entity.Role;
import com.dhl.cloud.core.entity.UserAndRole;

@Service
public class RoleService {

	@Autowired
	private UserAndRoleMapper userAndRoleMapper;
	@Autowired
	private RoleMapper roleMapper;
	

	public Boolean addUserRole(HttpUser user,String username, List<Integer> roleids) {
		userAndRoleMapper.deleteByUser(username,user.getTenantid());
		UserAndRole userAndRole;
		List<UserAndRole> userAndRoles = new ArrayList<>();
		for (Integer roleid:roleids){
			userAndRole=new UserAndRole();
			userAndRole.setUsername(username);
			userAndRole.setRoleid(roleid);
			userAndRole.setTenantid(user.getTenantid());
			userAndRoles.add(userAndRole);
		}
		userAndRoleMapper.insertList(userAndRoles);
		return true;
	}
	
	public Role getRole(Integer id)
	{
		Role r = roleMapper.selectByPrimaryKey(id);
		return r;
	}
	
	public List<Role> getRoleList()
	{
		return roleMapper.selectAll();
	}
}
